// pages/giftShare/giftShare.js
const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    card: [],
    total_num:0,
    getCardSuccess:false,
    getHopsSuccess:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log('参数',options)
    if (options.uid){
      var share_uid = options.uid
    }else{
      share_uid=''
    }
    if (options.shareType) {
      var shareType = options.shareType
      // console.log(shareType);
    } else {
      shareType = ''
    }
    wx.hideShareMenu() // 隐藏分享按钮
    var userinfo = wx.getStorageSync('accountInfo');
    console.log(userinfo);
    var uid = userinfo.uid;
    this.setData({
      show_type: options.show_type,
      linkCode: options.linkCode,
      uid:uid,
      share_uid: share_uid,
      getCardSuccess:false,
      shareType: shareType
    })
    // console.log(this.data)
    if (options.shareType==1){
      this.hopsInfo();
    }else{
      this.cardInfo()
    }
  },
// 酒花信息获取
hopsInfo:function(){
  var that = this;
  api.POST({
    params: {
      linkCode: that.data.linkCode
    },
    url: app.globalData.base_url + 'hops/statusInfo',
    success: function (res) {
      if (util.isEmpty(res.data.code)) {
        var hopsInfo = res.data.data.result;
        // console.log(card)
        that.setData({
          hopsInfo: hopsInfo
        })
        // that.doData(card.list);
      } else {
        util.showError(res.data.message);
      }
    },
    fail: function (fail) {
      util.showError("登录失败，请检查网络");
    }
  })
},

// 领取酒花
  getHops:function(){
    var that = this;
    api.POST({
      params: {
        linkCode: that.data.linkCode
      },
      url: app.globalData.base_url + 'hops/getHops',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          that.setData({
            getHopsSuccess: true
          })
        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");
      }
    })
  },

  toMyHops: function () {
    var sku_code = this.data.hopsInfo.sku_code;
    app.globalData.sku_code = sku_code;
    app.globalData.winType=2;
    wx.switchTab({
      url: '/pages/wineNew/wineNew'
    });
  },

  //礼品卡信息
  cardInfo: function () {
    var that = this;
    api.POST({
      params: {
        linkCode: that.data.linkCode
      },
      url: app.globalData.base_url + 'Giftcard/shareStatus',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          var card = res.data.data;
          // console.log(card)
          that.setData({
            card: card
          })
          that.doData(card.list);
        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");
      }
    })
  },

  goLink: function () {
    wx.switchTab({
      url: '/pages/index/index'
    });
  },

  getCard: function () {
    var that = this;
    api.POST({
      params: {
        linkCode: that.data.linkCode
      },
      url: app.globalData.base_url + 'Giftcard/shareSub',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          that.setData({
            getCardSuccess:true
          })
        } else if (res.data.code=='80021'){
          that.cardInfo();
          util.showError(res.data.message);
        }
         else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");
      }
    })
  },

  //处理数据
  doData: function (list) {
    var total_num=0;
    for (var i = 0; i < list.length; i++) {
      total_num += list[i].num
    }
    this.setData({
      total_num: total_num
    })
  },

  toMyCard:function(){
    app.globalData.linkCode = this.data.card.linkCode;
    wx.redirectTo({
      url: '/play/pages/myGift/myGift?show_type=2&card_type=2&cardLink=1'
    })
  },

  onShareAppMessage: function (res) {
    var that = this;
    var userinfo = app.globalData.accountInfo;
    var cardInfo = that.data.card;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: '好友"' + userinfo.nickname + '"送您"' + cardInfo.name + '"礼品卡',
        path: '/play/pages/giftShare/giftShare?uid=' + that.data.uid+'&show_type=4&linkCode=' + cardInfo.linkCode,
        imageUrl: cardInfo.share_img,
        success: function (res) {
          // 转发成功,修改状态为 分享
          // api.POST({
          //   params: {
          //     linkCode: cardInfo.linkCode,
          //   },
          //   url: app.globalData.base_url + 'Giftcard/shareSuccess',
          //   success: function (res) { 
          //     wx.redirectTo({
          //       url: '../myGift/myGift'
          //     })
          //   },
          //   fail:function(res){
          //     console.log('失败', res)
          //   }
          // })
        },
        fail: function (res) {
          // 转发失败
          console.log(res)
        }
      }
    }
  },
  winLink:function(e){
    var sku_code = e.currentTarget.dataset.sku_code;
    var show_type = this.data.show_type;
    app.globalData.sku_code = sku_code;
    app.globalData.winType=1;
    if (show_type==3){
      return
    }
    wx.switchTab({
      url: '/pages/wineNew/wineNew'
    });
  },
  //撤销礼品卡
  undo: function (e) {
    var that = this;
    var linkCode = that.data.card.linkCode
    wx.showModal({
      title: '提示',
      content: '是否确认撤销？',
      success(res) {
        if (res.confirm) {
          api.POST({
            params: {
              linkCode: linkCode,
            },
            url: app.globalData.base_url + 'giftcard/backout',
            success: function (res) {
              if (util.isEmpty(res.data.code)) {
                util.showSuccess("撤销成功");
                wx.navigateBack({
                  delta: 1
                })
              } else if (res.data.code=="80018"){
                util.showError(res.data.message);
                that.cardInfo()
              }
            }
          })
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
  }
})